package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.admin.DishService;
import com.sky.service.admin.SetmealService;
import com.sky.service.user.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wangqi
 * @date 2024/1/3 14:25
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1.创建一个ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //2.给ShoppingCart对象根据套餐、菜品分别设置name、image、number、amonut
        //再添加到购物车之前，先获取shopping_cart表中有没有dishId、setmealId对应的数据
        List<ShoppingCart> list =  shoppingCartMapper.findShoppingCartById(shoppingCart);
        log.info("===从购物车中查询到的结果：{}",list);
        if(list==null || list.size() ==0){
            //没有没有购物车数据，说明将是第一次添加
            shoppingCart.setNumber(1);
            //区分是添加套餐到购物车，还是添加菜品到购物车
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if(dishId!=null){
                //添加的是菜品，设置菜品的name、image、amonut
                DishVO dishVO = dishService.getDishById(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());
            }else if(setmealId!=null){
                //添加的是套餐，设置套餐的name、image、amonut
                SetmealVO setmealVO = setmealService.findSetmealById(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            //设置购物车数据创建时间、添加到购物车
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }else{
            shoppingCart = list.get(0);
            //如果有，说明以前添加过
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            //修改购物车数据
            shoppingCartMapper.update(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        //查询当前登录的用户的购物车数据
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.findShoppingCartByUserId(userId);
        return list;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //1.创建ShoppingCart对象，复制属性
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //2.减少shopping_cart中指定dish_id或者指定setmeal_id的number数量
        //先获取shopping_cart中要减少的菜品或者套餐
        List<ShoppingCart> list = shoppingCartMapper.findShoppingCartById(shoppingCart);
        if (list!=null && list.size()!=0){
            shoppingCart = list.get(0);
            //判断数量是否为1
            if (shoppingCart.getNumber() == 1){
                //删除该数据
                shoppingCartMapper.delete(shoppingCart);
            }else{
                //修改，将原来的数量-1
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.update(shoppingCart);
            }
        }
    }

    @Override
    public void deleteAll() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }
}
